package com.clinic.util;

import java.util.ArrayList;
import java.util.List;

import com.clinic.entity.db.Appointment;
import com.clinic.entity.db.Assistant;
import com.clinic.entity.db.Doctor;
import com.clinic.entity.db.Drug;
import com.clinic.entity.db.DrugBrand;
import com.clinic.entity.db.DrugIssue;
import com.clinic.entity.db.DrugStock;
import com.clinic.entity.db.Patient;
import com.clinic.entity.db.Prescription;
import com.clinic.entity.db.Syptom;
import com.clinic.entity.db.UserContact;
import com.clinic.entity.gui.GuiAppointment;
import com.clinic.entity.gui.GuiAssistant;
import com.clinic.entity.gui.GuiDoctor;
import com.clinic.entity.gui.GuiDrug;
import com.clinic.entity.gui.GuiDrugBrand;
import com.clinic.entity.gui.GuiDrugIssue;
import com.clinic.entity.gui.GuiDrugStock;
import com.clinic.entity.gui.GuiPatient;
import com.clinic.entity.gui.GuiPrescription;
import com.clinic.entity.gui.GuiSyptom;
import com.clinic.entity.gui.GuiUserContact;

public class ConvertUtil {
	
	public static GuiPatient getGuiPatientByPatient(Patient patient) {
		GuiPatient result = new GuiPatient();
		if(patient != null) {
			/**
			 * GuiPatient <-- Patient userId need.
			 * user id used in GUI to handle data
			 */
			// user common data
			result.setUserId(patient.getUserId());
			result.setName(patient.getName());
			result.setType(patient.getType().toString());
			result.setAdmin(patient.isAdmin());
			// patient additional data
			result.setDob(patient.getDob());
			if (patient.getGender() != null) {
				result.setGender(patient.getGender().toString());
			}
			if (patient.getDob() != null) {
				result.setAge(CommonUtil.getAgeByDob(patient.getDob()));
			}
		}
		return result;
	}
	
	public static Patient getPatientByGuiPatient(GuiPatient guiPatient) {
		return getPatientByGuiPatient(guiPatient, new Patient());
	}
	
	public static Patient getPatientByGuiPatient(GuiPatient guiPatient, Patient result) {
		if(guiPatient != null) {
			/**
			 * GuiPatient --> Patient userId no need.
			 * insert case : patient id auto generate.
			 * update case : patient id already exist in patient object.
			 */
			// user common data
			result.setName(guiPatient.getName());
			result.setType(guiPatient.getUserType());
			result.setAdmin(guiPatient.isAdmin());
			// patient additional data
			result.setDob(guiPatient.getDob());
			result.setGender(guiPatient.getGenderType());
		}		
		return result;
	}
	
	public static GuiDoctor getGuiDoctorByDoctor(Doctor doctor) {
		GuiDoctor result = new GuiDoctor();
		if(doctor != null) {
			// user common data
			result.setUserId(doctor.getUserId());
			result.setName(doctor.getName());
			result.setType(doctor.getType().toString());
			result.setAdmin(doctor.isAdmin());
			// doctor additional data
			result.setRegNo(doctor.getRegNo());
			result.setDesignation(doctor.getDesignation());
			result.setQualifications(doctor.getQualifications());
			result.setOthers(doctor.getOthers());
		}
		return result;
	}
	
	public static Doctor getDoctorByGuiDoctor(GuiDoctor guiDoctor) {
		return getDoctorByGuiDoctor(guiDoctor, new Doctor());
	}
	
	public static Doctor getDoctorByGuiDoctor(GuiDoctor guiDoctor, Doctor result) {
		if(guiDoctor != null) {
			// user common data
			result.setName(guiDoctor.getName());
			result.setType(guiDoctor.getUserType());
			result.setAdmin(guiDoctor.isAdmin());
			// doctor additional data
			result.setRegNo(guiDoctor.getRegNo());
			result.setDesignation(guiDoctor.getDesignation());
			result.setQualifications(guiDoctor.getQualifications());
			result.setOthers(guiDoctor.getOthers());
		}
		return result;
	}
	
	public static GuiAssistant getGuiAssistantByAssistant(Assistant assistant) {
		GuiAssistant result = new GuiAssistant();
		if(assistant != null) {
			// user common data
			result.setUserId(assistant.getUserId());
			result.setName(assistant.getName());
			result.setType(assistant.getType().toString());
			result.setAdmin(assistant.isAdmin());
			// assistant additional data
			result.setQualifications(assistant.getQualifications());
			result.setOthers(assistant.getOthers());
		}
		return result;
	}
	
	public static Assistant getAssistantByGuiAssistant(GuiAssistant guiAssistant) {
		return getAssistantByGuiAssistant(guiAssistant, new Assistant());
	}
	
	public static Assistant getAssistantByGuiAssistant(GuiAssistant guiAssistant, Assistant result) {
		if(guiAssistant != null) {
			// user common data
			result.setName(guiAssistant.getName());
			result.setType(guiAssistant.getUserType());
			result.setAdmin(guiAssistant.isAdmin());
			// assistant additional data
			result.setQualifications(guiAssistant.getQualifications());
			result.setOthers(guiAssistant.getOthers());
		}
		return result;
	}
	
	public static UserContact getUserContactByGuiUserContact(GuiUserContact guiContact, UserContact result) {
		if(guiContact != null) {
			result.setAddress(guiContact.getAddress());
			result.setTelNo(guiContact.getTelNo());
		}
		return result;
	}
	
	public static GuiUserContact getGuiUserContactByUserContact(UserContact contact) {
		GuiUserContact result = new GuiUserContact();
		if(contact != null) {
			result.setContactId(contact.getContactId());
			result.setAddress(contact.getAddress());
			result.setTelNo(contact.getTelNo());
		}
		return result;
	}
	
	public static GuiAppointment getGuiAppointmentByAppointment(Appointment appointment) {
		GuiAppointment guiAppointment = new GuiAppointment();
		if (appointment != null) {
			guiAppointment.setAppointmentId(appointment.getAppointmentId());
			guiAppointment.setCodeOrName(appointment.getPatient().getName());
			guiAppointment.setDate(CommonUtil.getDateStringFormatByDate(appointment.getDateAndTime()));
			guiAppointment.setTime(CommonUtil.getTimeStringFormatByDate(appointment.getDateAndTime()));
		}
		return guiAppointment;
	}
	
	public static GuiSyptom getGuiSyptomBySyptom(Syptom syptom) {
		GuiSyptom result = new GuiSyptom();
		if (syptom != null) {
			result.setSyptomId(syptom.getSyptomId());
			result.setName(syptom.getName());
		}
		return result;
	}
	
	public static GuiPrescription getGuiPrescriptionByPrescription(Prescription prescription) {
		GuiPrescription result = new GuiPrescription();
		if (prescription != null) {
			result.setPrescriptionId(prescription.getPrescriptionId());
			result.setPatient(ConvertUtil.getGuiPatientByPatient(prescription.getPatient()));
			List<GuiSyptom> guiSyptomList = new ArrayList<>();
			if (prescription.getSyptoms() != null) {
				for (Syptom syptom : prescription.getSyptoms()) {
					guiSyptomList.add(ConvertUtil.getGuiSyptomBySyptom(syptom));
				}
			}
			result.setSyptomList(guiSyptomList);
			List<GuiDrugIssue> guiDrugIssueList = new ArrayList<>();
			if (prescription.getIssues() != null) {
				for (DrugIssue drugIssue : prescription.getIssues()) {
					guiDrugIssueList.add(ConvertUtil.getGuiDrugIssueByDrugIssue(drugIssue));
				}
			}
			result.setDrugIssueList(guiDrugIssueList);
			result.setTerm(prescription.getVisitIndex() == 0 ? 1 : prescription.getVisitIndex());
			result.setTermString(CommonUtil.getTermStringByTerm(prescription.getVisitIndex() == 0 ? 1 : prescription.getVisitIndex()));
			result.setDiagnosticDescription(prescription.getDiagnosticDescription());
			result.setComment(CommonUtil.trim(prescription.getComment()));
			result.setDate(prescription.getDate());
		}
		return result;
	}
	
	public static GuiDrugStock getGuiDrugStockByDrugStock(DrugStock drugStock) {
		GuiDrugStock result = new GuiDrugStock();		
		result.setDrugStockId(drugStock.getDrugStockId());
		result.setOrderAmount(drugStock.getOrderAmount());
		result.setStockAmount(drugStock.getStockAmount());
		result.setExpireDate(drugStock.getExpireDate());
		result.setDrug(getGuiDrugByDrug(drugStock.getDrug()));
		result.setDaysLeft(CommonUtil.getDaysLeftfromToday(drugStock.getExpireDate()));
		return result;
	}
	
	public static GuiDrugBrand getGuiDrugBrandByDrugBrand(DrugBrand drugBrand) {
		GuiDrugBrand result = new GuiDrugBrand();
		result.setBrandId(drugBrand.getBrandId());
		result.setName(drugBrand.getName());
		return result;
	}
	
	public static GuiDrug getGuiDrugByDrug(Drug drug) {
		GuiDrug result = new GuiDrug();
		result.setDrugId(drug.getDrugId());
		result.setName(drug.getName());
		result.setWeight(drug.getWeight());
		result.setMinimum(drug.getMinimum());
		result.setBrand(getGuiDrugBrandByDrugBrand(drug.getBrand()));
		return result;
	}
	
	public static GuiDrugIssue getGuiDrugIssueByDrugIssue(DrugIssue drugIssue) {
		GuiDrugIssue result = new GuiDrugIssue();
		result.setDrugIssueId(drugIssue.getDrugIssueId());
		result.setTermsPerDay(drugIssue.getTermsPerDay());
		result.setNoOfDays(drugIssue.getNoOfDays());
		result.setAmount(drugIssue.getAmount());
		result.setTreatmentTime(drugIssue.getTreatmentTime());
		result.setIssue(drugIssue.isIssue());
		result.setDate(drugIssue.getDate());
		return result;
	}
	
	public static DrugIssue getDrugIssueByGuiDrugIssue(GuiDrugIssue guiDrugIssue) {
		return getDrugIssueByGuiDrugIssue(guiDrugIssue, new DrugIssue());
	}

	public static DrugIssue getDrugIssueByGuiDrugIssue(GuiDrugIssue guiDrugIssue, DrugIssue result) {
		result.setTermsPerDay(guiDrugIssue.getTermsPerDay());
		result.setNoOfDays(guiDrugIssue.getNoOfDays());
		result.setAmount(guiDrugIssue.getAmount());
		result.setTreatmentTime(guiDrugIssue.getTreatmentTime().getId());
		result.setIssue(guiDrugIssue.isIssue());
		result.setDate(guiDrugIssue.getDate());
		return result;
	}

	public static Drug getDrugByGuiDrug(GuiDrug guiDrug) {
		return getDrugByGuiDrug(guiDrug, new Drug());
	}

	public static Drug getDrugByGuiDrug(GuiDrug guiDrug, Drug result) {
		result.setName(guiDrug.getName());
		result.setWeight(guiDrug.getWeight());
		result.setMinimum(guiDrug.getMinimum());
		return result;
	}
	
	public static DrugBrand getDrugBrandByGuiDrugBrand(GuiDrugBrand guiDrugBrand) {
		return getDrugBrandByGuDrugBrand(guiDrugBrand, new DrugBrand());
	}
	
	public static DrugBrand getDrugBrandByGuDrugBrand(GuiDrugBrand guiDrugBrand, DrugBrand result) {
		result.setName(guiDrugBrand.getName());
		return result;
	}

	public static Syptom getSyptomByGuiSyptom(GuiSyptom guiSyptom) {
		Syptom result = new Syptom();
		result.setSyptomId(guiSyptom.getSyptomId());
		result.setName(guiSyptom.getName());
		return result;
	}
	
	public static List<GuiDrugBrand> getGuiDrugBrandsByDrugBrands(List<DrugBrand> drugBrandList) {
		List<GuiDrugBrand> result = new ArrayList<>();
		for (DrugBrand drugBrand : drugBrandList) {
			GuiDrugBrand guiDrugBrand = ConvertUtil.getGuiDrugBrandByDrugBrand(drugBrand);
			if (guiDrugBrand.isAvailable()) {
				result.add(guiDrugBrand);
			}
		}
		return result;
	}
	
}
